<?php
namespace app\admin\validate;

/**
 * 验证器
 */
use think\Validate;

class User extends Validate
{
    protected $rule = [
        'user_name' => 'require|max:25|unique:user|token', //checkName:user
        'password'  => 'require|max:25|min:5',
        'nickname'  => 'chsAlphaNum|max:25|min:2|unique:user',
        'email'     => 'email|unique:user',
        'captcha'   => 'require|captcha',
    ];

    protected $message = [
        'user_name.require'    => '名称必须',
        'user_name.max'        => '名称最多不能超过25个字符',
        'user_name.unique'     => '名称已经存在',
        'user_name.token'      => '数据异常,刷新后再试',

        'password.require'     => '密码必须',
        'password.max'         => '密码不能超过25',
        'password.min'         => '密码不能少于5',

        'nickname.chsAlphaNum' => '昵称只能是汉字、字母和数字',
        'nickname.max'         => '昵称不能超过15个',
        'nickname.min'         => '昵称不能少于2个',
        'nickname.unique'      => '昵称已经存在',

        'email.email'          => '邮箱格式错误',
        'email.unique'         => '邮箱已经存在',

        'captcha.require'      => '验证码要填写',
        'captcha.captcha'      => '验证码错误',
    ];

    // edit 验证场景定义
    public function sceneadd()
    {
        return $this->only(['user_name', 'password', 'nickname', 'email']);
    }

    public function sceneedit()
    {
        return $this->only(['password', 'nickname', 'email'])
            ->remove('password', 'require');
    }

    public function scenelogin()
    {
        return $this->only(['password', 'user_name', 'captcha'])->remove('user_name', 'unique');

    }

}
